// subcontract/Home/centralizedProcurement/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: false,//页面滚动的高度
    s: getApp().system.statusBarHeight, //状态栏高度
    n: (getApp().menu.top - getApp().system.statusBarHeight) * 2 + getApp().menu.height, //导航栏高度
    h: getApp().menu.height, //胶囊高度
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    progressPer: 0,
    activeIndex: 0,
    HotSellers8: [],
    page: 1,//页码
    pagingState: false,//分页状态
    goodsList: [],//商品
    LevelOne: [],//分类
  },
  /** 跳转 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  navigateBack() {
    wx.navigateBack()
  },
  /**tabs 切换 */
  onChange(event) {
    console.log(event);
    this.setData({
      activeIndex: event.detail.index
    })
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      goodsList: [],//商品
    })
    this.getpurchaseExplosives()
  },
  /**随机8个 */
  async getpurchaseExplosives8() {
    let object = {
      page_no: 1,
      page_size: '8',
      is_rand: 1
    }
    const { code, data } = await getApp().api.getpurchaseExplosives(object)
    if (code == 1) {
      this.setData({
        HotSellers8: data.lists
      })
    }
  },
  /** 一级分类 */
  async getLevelOneList() {
    const { code, data } = await getApp().api.getLevelOneList()
    if (code == 1) {
      this.setData({
        LevelOne: [{ id: '', name: '全部' }, ...data]
      })
      this.getpurchaseExplosives()
    }
  },
  /**商品列表 */
  async getpurchaseExplosives() {
    let object = {
      page_no: this.data.page,
      platform_cate_id: this.data.LevelOne[this.data.activeIndex].id
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getpurchaseExplosives(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  scrollBarChange(event) {
    let progressPer = 0
    let {
      scrollLeft,
      scrollWidth
    } = event.detail
    progressPer = (scrollLeft / (scrollWidth - 370)) * 100
    progressPer = Number(progressPer.toFixed(0))
    this.setData({
      progressPer
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getpurchaseExplosives8()
    this.getLevelOneList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getpurchaseExplosives()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onPageScroll(event) {
    this.setData({
      flag: event.scrollTop >= 200 ? true : false
    })
  },
})